﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace DBLibrary.Model.pojo
{
    public class SiteConfig : INotifyPropertyChanged,IDisposable      //站点配置表
    {

        private int _Id;                           //       [序号]  
        private int _isValid;                      //       [有效否]
        private int _BrachAdress;                  //       [分站地址]
        private string _BrachName;                 //       [分站名称]
        private string _installLocation;           //      ,[安装地点]
        private DateTime? _installTime;            //       [安装时间]
        private string _RunningState;              //      ,[运行状态]
        private int    _PowerOffCount;             //      ,[掉电次数统计]
        private DateTime? _LastPowerOffTime;       //      ,[最近掉电时间]
        private DateTime?  _BrachLastSetTime;      //       [分站上次整定时间]
        private int _CurSetPort;                   //      ,[当前整定端口序号]
        private int _isUpload;                     //      ,[上传否]
        private int _isUpload2;                    //      ,[上传否2]
        private int _isPowOffCur;                  //       [是否就地断电]
        private int _isBrachParaSet;               //      ,[分站参数整定否]
        private int _isInterlocked;                //      ,[是否风电瓦斯闭锁]
        private string _OpenPortName;              //      ,[开出端口名称]
        private string _Feedmonitoring;            //       [馈电关联监测]
        private int _InstallId;                    //      ,[安装地点编号]
        private int _isShield;                     //       [屏蔽否]
        private string _BrachfarlureState;         //     ,[分站故障状态]
        private string _commid;                    //      ,[commid]
        private int _isHistoryData;                //     ,[是否存在历史数据]
        private int _isPermitUpload;               //      ,[是否允许上传历史数据]
        private string _BrachIp;                   //      ,[分站IP]
        private string _Version;                   //     ,[版本]
        private string _switchSensors;              //开停传感器
        private int offlineDuration;               //     断线时常
        //新增充放电配置
        private int chargeDuration;    //充放电周期
        private DateTime? lastChargeTime; // 上次充放电时间
        private DateTime updateTime=DateTime.Now;  //最后收到数据的时间
        private int responseErrorTimes=0;//错误响应次数
        private int clockAdjustErrorTime = 0;//时钟校准错误次数
        private bool updated = false; //是否巡检完成

        private string maintainedStatus;          //   维检状况
        private int interlockedMode;             // 风电瓦斯闭锁模式
        private int? portStatus;
        private string powerOffMode;
        private List<InterActive> commands = new List<InterActive>();
 
        private string isReceived = "收到应答";                   //收到否,分站收到正常应答用 王晨 
        
        private float? LowLimitRange1;                        
        private float? LowLimitRange2;
        private float? LowLimitRange3;
        private float? LowLimitRange4;
        private float? LowLimitRange5;
        private float? LowLimitRange6;

        private bool rule1=false, rule2 = false, rule3 = false, rule4 = false, rule5 = false, rule6 = false,isEditing;
        private DateTime? rule1StartTime, rule2StartTime, rule3StartTime, rule4StartTime, rule5StartTime, rule6StartTime;
        private DateTime? rule1EndTime, rule2EndTime, rule3EndTime, rule4EndTime, rule5EndTime, rule6EndTime;
        private string message1,message2,message3,message4,message5,message6;
        private int downloading;//下载固件状态: 0 未下载，1 请求确认，可以下载，2：下载中
        public bool IsEditing {
            get
            {
                return isEditing;
            }
            set
            {
                isEditing = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsEditing"));
                }
            }
        }
        public bool isAdd = false;
        public int ChargeAction; //0,无动作,1:充电，2：放电, -1充放电失败（失败后不在下发指令）
        byte portControlStatus;
        double voltage;
        public object MemberCopy()
        {
            return this.MemberwiseClone();
        }
            
        public string IsReceived  
        {
            get => isReceived;
            set => isReceived = value;
        }

        public int? PortsStatus //端口状态，分站运行状态用，赵金东
        {
            get
            {
                if (portStatus == null)
                    return 0;
                else
                    return  portStatus;
            }
            set {
                portStatus = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PortsStatus"));
                }
            }
        }

        public int Id
        {
            get => _Id;
            set
            {
                _Id = value;
                if (PropertyChanged != null)
                {

                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Id"));
                }
            }
        }
        public int IsValid
        {
            get => _isValid;
            set
            {
                _isValid = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsValid"));
                }
            }
        }
        public int BrachAdress
        {
            get => _BrachAdress;
            set
            {
                _BrachAdress = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachAdress"));
                }
            }
        }
        public string BrachName
        {
            get => _BrachName;
            set
            {
                _BrachName = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachName"));
                }
            }
        }
        public string InstallLocation
        {
            get => _installLocation;
            set
            {
                _installLocation = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InstallLocation"));
                }
            }
        }
        public DateTime? InstallTime
        {
            get => _installTime;
            set
            {
                _installTime = value;
                if (PropertyChanged != null)
                {
                   //PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InstallTime"));
                }
            }
        }
        public string RunningState
        {
            get {
                if (_RunningState == string.Empty|| _RunningState==null)
                    return _BrachfarlureState==null?"": _BrachfarlureState;
                else
                    return _RunningState;
            }
            set
            {
                _RunningState = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("RunningState"));
                }
            }
        }
        public int PowerOffCount
        {
            get => _PowerOffCount;
            set
            {
                _PowerOffCount = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PowerOffCount"));
                }
            }
        }
        public DateTime? LastPowerOffTime
        {
            get
            {
                if(_LastPowerOffTime == null && RunningState.Contains("直流"))
                    return DateTime.Now;
                else
                    return _LastPowerOffTime;
            }
            set
            {
                _LastPowerOffTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("LastPowerOffTime"));
                }
            }
        }
        public DateTime? BrachLastSetTime
        {
            get => _BrachLastSetTime;
            set
            {
                _BrachLastSetTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachLastSetTime"));
                }
            }
        }
        public int CurSetPort
        {
            get => _CurSetPort;
            set
            {
                _CurSetPort = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurSetPort"));
                }
            }
        }
        public int IsUpload
        {
            get => _isUpload;
            set
            {
                _isUpload = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsUpload"));
                }
            }
        }
        public int IsUpload2
        {
            get => _isUpload2;
            set
            {
                _isUpload2 = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsUpload2"));
                }
            }
        }
        public int IsPowOffCur {
            get => _isPowOffCur;
            set
            {
                _isPowOffCur = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsPowOffCur"));
                }
            }
        }
        public int IsBrachParaSet
        {
            get => _isBrachParaSet;
            set
            {
                _isBrachParaSet = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsBrachParaSet"));
                }
            }
        }
        public int IsInterlocked
        {
            get => _isInterlocked;
            set
            {
                _isInterlocked = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsInterlocked"));
                }
            }
        }
        public string OpenPortName
        {
            get => _OpenPortName;
            set
            {
                _OpenPortName = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OpenPortName"));
                }
            }
        }
        public string Feedmonitoring
        {
            get => _Feedmonitoring;
            set
            {
                _Feedmonitoring = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Feedmonitoring"));
                }
            }
        }
        public int InstallId
        {
            get => _InstallId;
            set
            {
                _InstallId = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InstallId"));
                }
            }
        }
        public int IsShield
        {
            get => _isShield;
            set
            {
                _isShield = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsShield"));
                }
            }
        }
        public string BrachfarlureState
        {
            get => _BrachfarlureState;
            set
            {
                _BrachfarlureState = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachfarlureState"));
                }
            }
        }
        public string Commid
        {
            get => _commid;
            set
            {
                _commid = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Commid"));
                }
            }
        }
        public int IsHistoryData
        {
            get => _isHistoryData;
            set
            {
                _isHistoryData = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsHistoryData"));
                }
            }
        }
        public int IsPermitUpload
        {
            get => _isPermitUpload;
            set
            {
                _isPermitUpload = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsPermitUpload"));
                }
            }
        }
        public string BrachIp
        {
            get => _BrachIp;
            set
            {
                _BrachIp = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachIp"));
                }
            }
        }
        public string Version
        {
            get {
                if (string.IsNullOrEmpty(_Version))
                    return "6.31";
                else
                    return _Version;
            }
            set
            {
                _Version = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Version"));
                }
            }
        }
        public int OfflineDuration
        {
            get => offlineDuration;
            set
            {
                offlineDuration = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OfflineDuration"));
                }
            }
        }
        public DateTime UpdateTime
        {
            get => updateTime;
            set
            {
                updateTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UpdateTime"));
                }
            }
        }


        public int ResponseErrorTimes { get => responseErrorTimes; set => responseErrorTimes = value; }

        public int InterlockedMode
        {
            get => interlockedMode;
            set
            {
                interlockedMode = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InterlockedMode"));
                }
            }
        }

        public int ClockAdjustErrorTime { get => clockAdjustErrorTime; set => clockAdjustErrorTime = value; }
        public bool Updated { 
            get => updated;
            set
            {
                updated = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ClockAdjustErrorTime"));
                }
            }
        }
        public string MaintainedStatus
        {
            get => maintainedStatus;
            set
            {
                maintainedStatus = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MaintainedStatus"));
                }
            }
        }

        public string PowerOffMode { 
            get
            {
                if (IsPowOffCur == 1)
                    return "就地断电";
                else
                    return "系统断电";
            }
            set { 
                powerOffMode = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PowerOffMode"));
                }
            }
        }

        public List<InterActive> Commands { 
            get => commands; 
            set { 
                commands = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Commands"));
                }
            } 
        }
        public override string ToString()
        {
            return BrachAdress + "号分站-" + BrachName+ "-"+ InstallLocation;
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public float? LowLimitRange11 { get => LowLimitRange1; set => LowLimitRange1 = value; }
        public float? LowLimitRange21 { get => LowLimitRange2; set => LowLimitRange2 = value; }
        public float? LowLimitRange31 { get => LowLimitRange3; set => LowLimitRange3 = value; }
        public float? LowLimitRange41 { get => LowLimitRange4; set => LowLimitRange4 = value; }
        public float? LowLimitRange51 { get => LowLimitRange5; set => LowLimitRange5 = value; }
        public float? LowLimitRange61 { get => LowLimitRange6; set => LowLimitRange6 = value; }
        public bool IsAdd { get => isAdd; set => isAdd = value; }
        public int ChargeDuration { get => chargeDuration;
            set { 
                chargeDuration = value;
                IsEditing = true;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Commands"));
                }
            }
        }
        public DateTime? LastCHargeTime { get => lastChargeTime; set
            {
                lastChargeTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InterlockedMode"));
                }
            }
        }

        public bool Rule1 { get => rule1; set => rule1 = value; }
        public bool Rule2 { get => rule2; set => rule2 = value; }
        public bool Rule3 { get => rule3; set => rule3 = value; }
        public bool Rule4 { get => rule4; set => rule4 = value; }
        public bool Rule5 { get => rule5; set => rule5 = value; }
        public bool Rule6 { get => rule6; set => rule6 = value; }
        public DateTime? Rule1StartTime { get => rule1StartTime; set => rule1StartTime = value; }
        public DateTime? Rule2StartTime { get => rule2StartTime; set => rule2StartTime = value; }
        public DateTime? Rule3StartTime { get => rule3StartTime; set => rule3StartTime = value; }
        public DateTime? Rule4StartTime { get => rule4StartTime; set => rule4StartTime = value; }
        public DateTime? Rule5StartTime { get => rule5StartTime; set => rule5StartTime = value; }
        public DateTime? Rule6StartTime { get => rule6StartTime; set => rule6StartTime = value; }
        public DateTime? Rule1EndTime { get => rule1EndTime; set => rule1EndTime = value; }
        public DateTime? Rule2EndTime { get => rule2EndTime; set => rule2EndTime = value; }
        public DateTime? Rule3EndTime { get => rule3EndTime; set => rule3EndTime = value; }
        public DateTime? Rule4EndTime { get => rule4EndTime; set => rule4EndTime = value; }
        public DateTime? Rule5EndTime { get => rule5EndTime; set => rule5EndTime = value; }
        public DateTime? Rule6EndTime { get => rule6EndTime; set => rule6EndTime = value; }
        public string Message1 { get => message1; set => message1 = value; }
        public string Message2 { get => message2; set => message2 = value; }
        public string Message3 { get => message3; set => message3 = value; }
        public string Message4 { get => message4; set => message4 = value; }
        public string Message5 { get => message5; set => message5 = value; }
        public string Message6 { get => message6; set => message6 = value; }
        public int Downloading { get => downloading; set => downloading = value; }
        public byte PortControlStatus { get => portControlStatus; set => portControlStatus = value; }
        public double Voltage { get => voltage;
            set
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Voltage"));
                }
            } 
        }
        public string SwitchSensors { get => _switchSensors; set => _switchSensors = value; }
        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

    }
}